package com.example.xd.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.model.RouteMeta;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.xd.commen.annotation.FloatProcess;
import com.example.xd.model_permission.DefaultCallPermission;
import com.example.xd.model_permission.NotificationsUtils;
import com.example.xd.model_permission.float_window_permission.FloatWindowManager;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @auther XuDan on 2019/7/4.
 */
@Interceptor(priority = 5)
public class FloatInterceptor implements IInterceptor {
    Context context;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        float_method(postcard, callback);
//        notifiction_method(postcard, callback);
//        call_method(postcard, callback);

    }

    private void call_method(Postcard postcard, InterceptorCallback callback) {
        if (FloatProcess.isHave(getClazz(postcard))) {
            if (DefaultCallPermission.getInstance(context).isDefaultPhoneCallApp()) {
                callback.onContinue(postcard);
            } else {
                DefaultCallPermission.getInstance(context).setDefaultPhoneCall();
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    private void notifiction_method(Postcard postcard, InterceptorCallback callback) {
        if (FloatProcess.isHave(getClazz(postcard))) {
            if (!NotificationsUtils.isNotificationEnabled(context)) {
                Log.i("xd----------", "通知栏权限需要获取");
                Bundle extras = postcard.getExtras();
                ARouter.getInstance()
                        .build("/myapplication/NotifictionDialogActivity")
                        .withBundle("extras", extras)
                        .withString("path", postcard.getPath())
                        .withString("type", "notic")
                        .navigation();
            } else {
                Log.i("xd----------", "通知栏权限已存在");
                callback.onContinue(postcard);
            }
        } else {
            callback.onContinue(postcard);

        }
    }

    private void float_method(Postcard postcard, InterceptorCallback callback) {
        if (FloatProcess.isHave(getClazz(postcard))) {
            if (FloatWindowManager.getInstance().checkPermission(context)) {
                callback.onContinue(postcard);
            } else {
                Bundle extras = postcard.getExtras();
                ARouter.getInstance()
                        .build("/myapplication/NotifictionDialogActivity")
                        .withBundle("extras", extras)
                        .withString("path", postcard.getPath())
                        .withString("type", "float")
                        .navigation();
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        this.context = context;
    }

    public Class getClazz(Postcard postcard) {
        Map<String, RouteMeta> routes = null;
        if (routes == null) {
            Class<?> aClass = null;
            try {
                aClass = Class.forName("com.alibaba.android.arouter.core.Warehouse");
                Field routes1 = aClass.getDeclaredField("routes");
                routes1.setAccessible(true);
                routes = (Map<String, RouteMeta>) routes1.get(null);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        RouteMeta routeMeta = routes.get(postcard.getPath());
        Class activityClass = routeMeta.getDestination();
        return activityClass;
    }
}
